function score_curve = evaluateVoting(config,...
    dataset_i , loss_test_est, classifier_id , classifier_bank_path,  voting_method, if_save)

    if nargin<7
        if_save = 0;
    end
    

    try
        timer = toc;
    catch
        tic;
        timer = toc;
    end

    frame_n = size( loss_test_est, 1);
    
    classifier_list = genClassifierList( classifier_id, classifier_bank_path );
    
    dataset_name = config.datasets{ dataset_i }.name;
    
    disp(['dataset ' dataset_name]);
    
    res_pool_root = './temp/res_pool/';
    
    if strcmpi( config.res_type, config.datasets{dataset_i}.src_type )
        res_format = @(src_name)src_name;
    else
        n_str_src = length( config.datasets{dataset_i}.src_type );
        res_format = @(src_name)[src_name(1:end- n_str_src-1) config.res_type];
    end
    
    if if_save > 0
        checkFolder( './result/' );
        res_folder_name = [ './result/' dataset_name '/' ];
        checkFolder( res_folder_name );
    end
            
    name_format = @( classifier_name , res_name )([res_pool_root, dataset_name,'/', classifier_name, '/' , res_name]);
    
    if strcmpi( voting_method , 'Uniform')
        loss2weight = @(loss)(1);
    elseif strcmpi( voting_method , 'Inverse')
        loss2weight = @(loss)(1/max(loss,0.1));
    elseif strcmpi( voting_method , 'LogOdd')
        loss2weight = @(loss)( log( 1.01-loss ) - log(loss+.01) );
    end
    
    classifier_n = size( loss_test_est, 2);
    
    max_classifier_n = round( classifier_n*.3);
    if_save = min( max_classifier_n, if_save );
    
    ts = zeros( 1, max_classifier_n);
    ps = zeros( 1, max_classifier_n);
    tps = zeros( 1, max_classifier_n);
    score_curve = zeros( 1, max_classifier_n);
    
    start_timer = toc;
    timer = toc;
    
    for i = 1:frame_n
        
        if (toc-timer>20)
            disp(['rest time for dataset ' dataset_name ' is ' ...
                num2str( (toc - start_timer ) / i * (frame_n - i) ) ...
                ' for now max F score is ' ...
                num2str( max( score_curve) ) ]);
            timer = toc;
        end
        %loss_test_est
        vals = loss_test_est( i , : );
        [~,sort_id] = sort( vals );
        
        src_name = config.datasets{ dataset_i }.src_list{i};
        
        gt_name = [config.datasets{ dataset_i }.gt_root config.datasets{ dataset_i }.gt_list{i}];
        
        gt = checkType( imread( gt_name ) );
        %gt = rand(300,300);                
        
        sum_res = zeros( size(gt) , 'double');
        
        sum_weight = 0;
        
        for j = 1:max_classifier_n            
            weight_j = loss2weight( vals( sort_id(j) ) );
            sum_weight = sum_weight + weight_j;
            
            res_j = checkType( imread(name_format( classifier_list{ sort_id(j) },src_name ) )); 
            %res_j = rand( 300,300);
            if(j==1)
                sum_res = zeros( size(res_j) , 'double');
            end
            
            sum_res = sum_res + res_j;
            [tp,p,t] = evaluateFrame( gt, sum_res / sum_weight );  
            
            if if_save == j
                res_save_name = [ res_folder_name src_name ];
                imwrite( uint8(sum_res / sum_weight*255), res_save_name );
            end
            tps(j) = tps(j)+tp;
            ps(j) = ps(j) + p;
            ts(j) = ts(j) + t;
        end         
        
        precision = tps./(1e-3+ps);
        recall = tps./(1e-3+ts);
        score_curve = 2 * precision.*recall ./( precision+1e-3+recall);
        
        %break;
    end        
    disp(['final score ' num2str( max( score_curve ) )]);
end

function [tp, p, t] = evaluateFrame( gt, res )
    gt = imresize(gt, [300, 300] );
    res = imresize(res, [300, 300] );
    gt = gt(:);
    res = res(:);
    bo_gt = double(gt>.5);
    bo_res = double(res>.5);
    tp = sum( double(bo_gt.*bo_res) );
    t = sum(bo_gt);
    p = sum(bo_res);
end

function img = checkType( img )
    if( size( img , 3)==3)
        img = rgb2gray( img );
    end
    img = im2double( img );    
end

function classifier_list = genClassifierList( classifier_id, classifier_bank_path )
    for i = 1:length( classifier_id )
        id = classifier_id( i );
        name = classifier_bank_path( id ).name;
        sep = find( name == '_' );
        name = name( 1:sep(end-1)-1 );
        classifier_list{i} = name;
    end
end